package exttable.usingTableModelWithFindCell;

import exttable.dbclasses.DbFile;
import exttable.formgenerator.MessageFactory;
import options.AppSettings;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class JButtonTableCellRenderer implements TableCellRenderer{
	private int[]cellCoordinate;

	public int[] getCellCoordinate() {
		return cellCoordinate;
	}

	public void setCellCoordinate(int[] cellCoordinate) {
		this.cellCoordinate = cellCoordinate;
	}
	

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if(Arrays.equals(cellCoordinate, new int[]{row,column})){
			JButton jButton=new JButton();
            DbFile dbFile=DbFile.getDbFileById(Integer.valueOf(value.toString()));
			jButton.setText("view file "+dbFile.getFileName());
            jButton.setName(value.toString());
			if(!hasFocus){
				jButton.setBorder(new LineBorder(null, 0));
			}
			return jButton;
		}
		return new DefaultTableCellRenderer().getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	}
	
	public void setOpenFileMouseClicked(JTable jTable){
		jTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JTable jTable=(JTable)e.getSource();
				if(Arrays.equals(cellCoordinate, new int[]{jTable.getSelectedRow(),jTable.getSelectedColumn()})){
					Desktop desktop=Desktop.getDesktop();
					try {
                        String fileId=jTable.getCellRenderer(jTable.getSelectedRow(),jTable.getSelectedColumn()).getTableCellRendererComponent(jTable, jTable.getValueAt(jTable.getSelectedRow(),jTable.getSelectedColumn()), true, true, jTable.getSelectedRow(),jTable.getSelectedColumn()).getName();
//                        String fileId=(String)jTable.getValueAt(cellCoordinate[0],cellCoordinate[1]);
//                        fileId.replace("view file ","");
                        DbFile dbFile=DbFile.getDbFileById(Integer.valueOf(fileId));
                        String pathToDir=dbFile.getFileName().split("_")[0]+"\\"+dbFile.getTableName()+"\\"+dbFile.getFileName()+"."+dbFile.getFileEhtension();
                        desktop.open(new File(AppSettings.get("generalSettings", "saveFileTo", "").toString()+"\\"+pathToDir));
					}
                    catch (IllegalArgumentException iae){
                        MessageFactory.getMessage(iae.getMessage());
                    }
					catch (FileNotFoundException e1) {
						e1.printStackTrace();
					}
					catch (IOException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
				}
			}
		});
	}

}
